const bodyParser = require('body-parser')
const Mock = require('mockjs')

const { list } = Mock.mock({
  "list|10": [{
    "title": "@ctitle",
    "desc": "@cword(10, 20)",
    "image": "@image(120x80, @color)",
    "id": "@id"
  }]
})
module.exports = (app) => {
  // 必须使用中间件 bodyParser.json ， 否则 req.body 将会是undefined
  app.use(bodyParser.json())
  // 用户列表
  const userList = [
    {
      username: 'admin',
      password: 'admin'
    },
    {
      username: 'shijiale',
      password: 'shijiale'
    }
  ]
  // 登录接口
  app.post("/api/login", (req, res) => {
    // 获取用户名和密码
    const { username, password } = req.body;
    // 查找用户名和密码
    const user = userList.find(v => v.username === username && v.password === password);

    // 判断
    if(user) {
      // 验证通过
      res.send({
        code: 200,
        message: "登录成功",
      });

    } else {
      // 验证不通过
      res.send({
        code: 400,
        message: "账户密码错误， 请重新输入",
      });
    }
  });
  
  // 列表接口
  app.get('/api/list', (req, res) => {
    res.send({
      code: 200,
      data: list,
      message: 'ok'
    })
  })
};
